package org.fing.edu.uy.admin.push;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.push.PushContext;
import org.primefaces.push.PushContextFactory;

@ManagedBean
@ApplicationScoped
public class GlobalCounter implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private long countBPS;
	private long countDGI;
	private long countDNIC;
	
	private long lastActive;

	@PostConstruct
	public void init(){
		ApplicationManagedSingleton.getInstance().setBeanInstance(this);		
		reset();
	}
	
	public void reset(){
		setCountBPS(0);
		setCountDGI(0);
		setCountDNIC(0);
		setLastActive(-1);
	}

	public synchronized void increment(long serviceID) {
		long cant = 0;
		if(serviceID==1){
			setCountBPS(getCountBPS() + 1);
			cant = getCountBPS();
		}else if (serviceID ==2){
			setCountDGI(getCountDGI() + 1);
			cant = getCountDGI();
		}else if ( serviceID == 3) {
			setCountDNIC(getCountDNIC() + 1);
			cant = getCountDNIC();
		}
		
		PushContext pushContext = PushContextFactory.getDefault().getPushContext();
		pushContext.push("/counter", String.valueOf(serviceID) + ";" + String.valueOf(cant));

	}

	public long getLastActive() {
		return lastActive;
	}

	public void setLastActive(long lastActive) {
		this.lastActive = lastActive;
	}

	public long getCountDNIC() {
		return countDNIC;
	}

	public void setCountDNIC(long countDNIC) {
		this.countDNIC = countDNIC;
	}

	public long getCountDGI() {
		return countDGI;
	}

	public void setCountDGI(long countDGI) {
		this.countDGI = countDGI;
	}

	public long getCountBPS() {
		return countBPS;
	}

	public void setCountBPS(long countBPS) {
		this.countBPS = countBPS;
	}

}
